package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.jianchuanghua.JianChuangHuaMgr;
import com.yanqu.road.server.manger.activity.ppl.PplMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.system.CommonMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

public class PplRecycleStrategy extends AbstractItemRecycleStrategy {
    private final int RECOVERY_ITEM_TYPE = 1;
    @Override
    public void markNeedRecycleItem() {
        ActivityInfo activityInfo = PplMgr.getActivityInfo();
        if (activityInfo != null) {
            long recycleTime = activityInfo.getEndShowTime() * 1000 + 600 * 1000;
            ActivityItemRecycleMgr.addRecycleItemData(PplMgr.PPL_BOOK_MOUNTAIN_RIVER_ID, activityType, recycleTime);
            ActivityItemRecycleMgr.addRecycleItemData(PplMgr.PPL_BOOK_FISHING_SONG_ID, activityType, recycleTime);
            ActivityItemRecycleMgr.addRecycleItemData(eItemId.PPL_COMMON_BOX.getValue(), activityType, recycleTime);
            ActivityItemRecycleMgr.addRecycleItemData(eItemId.PPL_HIGH_BOX.getValue(), activityType, recycleTime);

            List<String> paramList = new ArrayList<>();
            paramList.add(String.valueOf(PplMgr.PPL_SCORE_GOODS_ID));
            paramList.add(activityInfo.getActivityName());
            ActivityItemRecycleMgr.addRecycleItemData(PplMgr.PPL_ENERGY_RECOVERY_ID, activityType,RECOVERY_ITEM_TYPE,activityInfo.getEndTime() * 1000 + 600 * 1000,paramList);
        }
    }

    @Override
    public boolean isActivityRunning() {
        ActivityInfo activityInfo = PplMgr.getActivityInfo();
        return ActivityMgr.activityInTime(activityInfo);
    }

    @Override
    protected void initActivityType() {
        activityType = eActivityType.PplActivity.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        int itemId = data.getItemId();
        int recycleType = data.getRecycleType();
        if (recycleType == RECOVERY_ITEM_TYPE) {
            // 体力道具特殊处理
            recycleRecoveryItem(userId, data);
        }else {
            UserBagItem userBagItem = getUserBagItem(userId, itemId);
            if (userBagItem != null && userBagItem.getNum() > 0) {
                removeItem(userBagItem);
            }
        }
    }

    private void recycleRecoveryItem(long userId, ActivityItemRecycleData data) {
        int itemId = data.getItemId();
        UserBagItem userBagItem = getUserBagItem(userId,itemId);
        if (userBagItem == null) {
            return;
        }
        GoodsInfo goodsInfo = CommonMgr.getGoodsInfo(itemId);
        List<String> paramList = data.getParamList();
        int scoreId = Integer.parseInt(paramList.get(0));
        GoodsInfo goodsInfo2 = CommonMgr.getGoodsInfo(scoreId);
        if (goodsInfo == null || goodsInfo2 == null) {
            log.error("changePplActivityItem error! UserBagItem or GoodsInfo is null!");
            return;
        }
        String language = UserMgr.getUserInfo(userId).getLanguage();
        String activityName =  ServerLanguageMgr.getContent(paramList.get(1), language);
        String goodsName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(),language);
        String goodsName2 = ServerLanguageMgr.getContent(goodsInfo2.getGoodsName(),language);
        if (userBagItem.getNum() > 0) {
            long num = userBagItem.getNum();
            if (removeItem(userBagItem)) {
                Property property = new Property(PplMgr.PPL_SCORE_GOODS_ID, BigInteger.valueOf(num * goodsInfo.getParamList().get(1).intValue()));
                String title = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.ACTIVITY_GOODS_AUTO_CONSUME_TITLE, language), activityName);
                String content = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.ACTIVITY_GOODS_AUTO_CONSUME_CONTENT,language),
                        activityName, goodsName, num, goodsName2);
                MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(property), content, title);
            }
        }
    }
}
